package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ApplyMessagebean;
import com.wnhz.work.yz.bean.InfoLevelBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---新增来访人
 */
public class AddPeoeleActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private TextView tv_right;

    private EditText et_name, et_position, et_phone;
    private TextView tv_sex;

    private TextView tv_choose;

    // private List<String> memberList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_peoele);
        initData();
        getPersonLevel();

    }

    private List<InfoLevelBean.InfoEntity> info1 = new ArrayList<>();

    private void getPersonLevel() {
        Map<String, Object> map = new HashMap<>();
        showDialog();

        XUtil.Post(Url.INFORMATION_LEVELNAME, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        InfoLevelBean bea = gson.fromJson(result, InfoLevelBean.class);
                        info1 = bea.getInfo();

                        initView();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    int index = -1;
    private ApplyMessagebean.CustomInfoEntity c = new ApplyMessagebean.CustomInfoEntity();
    private List<ApplyMessagebean.CustomInfoEntity> cust = new ArrayList<>();

    private void initData() {
//        memberList.add("一般客户");
//        memberList.add("重要客户");
        index = getIntent().getIntExtra("index", -1);
        cust = ApplyMessageActivity.bean.getCustom_info();
        if (index != -1)
            c = cust.get(index);

    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("新增来访人");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("保存");
        tv_right.setTextColor(0xff008c99);


        et_name = (EditText) findViewById(R.id.et_name);
        et_position = (EditText) findViewById(R.id.et_position);
        tv_sex = (TextView) findViewById(R.id.tv_sex);
        et_phone = (EditText) findViewById(R.id.et_phone);

        tv_choose = (TextView) findViewById(R.id.tv_choose);
        if (index != -1) {
            et_name.setText("" + c.getCustomer());
            et_position.setText(c.getPosition());
            String sex = "男";
            if ("1".equals(c.getSex())) {
                sex = "男";
            } else if ("0".equals(c.getSex())) {
                sex = "女";
            } else {
                sex = "未知";
            }
            tv_sex.setText(sex);
            et_phone.setText(c.getTel());
            //MyToast("请选择等级" + c.getLevel_name());
            tv_choose.setText(c.getLevel_name());
        }

        if (ApplyMessageActivity.bean.getCanUpdata().equals("0")) {
            et_name.setEnabled(false);
            et_position.setEnabled(false);
            et_phone.setEnabled(false);
        } else {
            findViewById(R.id.rl_member).setOnClickListener(this);//会员等级

            findViewById(R.id.rl_sex).setOnClickListener(this);//性别
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //保存
            case R.id.submit:
                String name = et_name.getText().toString().trim();
                String position = et_position.getText().toString().trim();
                String sex = tv_sex.getText().toString().trim();
                String phone = et_phone.getText().toString().trim();
                String choose = tv_choose.getText().toString().trim();
                if ("".equals(name)) {
                    MyToast("请填写姓名");
                    return;
                }
                if ("".equals(position)) {
                    MyToast("请填写职位");
                    return;
                }
                if ("".equals(sex)) {
                    MyToast("请选择性别");
                    return;
                }
                if ("".equals(phone)) {
                    MyToast("请填写联系电话");
                    return;
                }
                if ("".equals(choose)) {
                    MyToast("请选择等级");
                    return;
                }
                if (phone.length() != 11) {
                    MyToast("请输入11位号码");
                    return;
                }


                //  MyToast("保存");
                c.setCustomer(name);
                c.setLevel_name(choose);
                c.setPosition(position);

                String sexId = "0";
                if (sex.equals("男")) {
                    sexId = "1";
                } else if (sex.equals("女")) {
                    sexId = "0";
                } else if (sex.equals("未知")) {
                    sexId = "2";
                }
                c.setSex(sexId);
                c.setTel(phone);
                if (index != -1) {
                    cust.set(index, c);
                } else {
                    cust.add(c);
                }
                ApplyMessageActivity.bean.setCustom_info(cust);
                setResult(RESULT_OK);
                finish();

                break;

            //客户等级
            case R.id.rl_member: {
                final List<String> str = new ArrayList<>();
                for (int i = 0; i < info1.size(); i++) {
                    str.add(info1.get(i).getLevel_name());
                }
                OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        tv_choose.setText(str.get(options1));
                        c.setLevel_name(str.get(options1));
                        c.setLevel_id(info1.get(options1).getId());
                    }
                })
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .build();
                pvOptions.setPicker(str);
                pvOptions.show();
            }

            break;
            case R.id.rl_sex:
                final List<String> str = new ArrayList<>();
                str.add("男");
                str.add("女");
                str.add("未知");
                OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        tv_sex.setText(str.get(options1));

                    }
                })
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .build();
                pvOptions.setPicker(str);
                pvOptions.show();
                break;

            default:
                break;
        }
    }


}
